package com.mall.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.dao.mapper.OrderMapper;
import com.mall.dao.mapper.StaffMapper;
import com.mall.model.Order;
import com.mall.model.OrderExample;
import com.mall.model.Staff;
import com.mall.model.StaffExample;
import com.mall.model.StaffExample.Criteria;

@Service
public class StaffService {
	@Autowired
	private StaffMapper staffMapper;
	@Autowired
	private OrderMapper orderMapper;
	public StaffMapper getStaffMapper() {
		return staffMapper;
	}

	public void setStaffMapper(StaffMapper staffMapper) {
		this.staffMapper = staffMapper;
	}

	/**
	 * 获取员工列表
	 *
	 * @dagewang 2017年8月19日
	 */
	public List<Staff> listStaff() throws Exception {
		List<Staff> list = staffMapper.selectByExample(new StaffExample());
		return list;
	}

	/**
	 * 获取单个员工
	 *
	 * @dagewang 2017年8月19日
	 */
	public Staff get(Integer id) {
		Staff staff = staffMapper.selectByPrimaryKey(id);
		return staff;
	}

	/**
	 * 登录
	 *
	 * @dagewang 2017年9月25日
	 */
	public Staff getByName(String username, String password) {
		StaffExample ex = new StaffExample();
		Criteria createCriteria = ex.createCriteria();
		createCriteria.andUsernameEqualTo(username);
		createCriteria.andPasswordEqualTo(password);
		List<Staff> selectByExample = staffMapper.selectByExample(ex);
		if (selectByExample.size() != 0) {
			return selectByExample.get(0);
		}
		return null;
	}
	/**
	 * 分配给指定配送员的订单
	 *
	 *@dagewang 2017年10月24日
	 */
	public List<Order> getOrders(Integer id) {
		OrderExample ex=new OrderExample();
		com.mall.model.OrderExample.Criteria createCriteria = ex.createCriteria();
		createCriteria.andStaffidEqualTo(id);
		createCriteria.andStateEqualTo(2);
		ex.setOrderByClause("cdate desc");
		List<Order> selectByExample = orderMapper.selectByExample(ex);
		if(selectByExample.size()==0){
			selectByExample=null;
		}
		return selectByExample;
	}
	//修改信息
	public int update(Staff staff)throws Exception {
		return staffMapper.updateByPrimaryKeySelective(staff);
	}

}
